package org.zht.dormpro.dorm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.cacher.ICollegeCacher;
import org.zht.dormpro.dorm.mapper.TbCollegeMapper;
import org.zht.dormpro.dorm.model.TbCollege;
import org.zht.dormpro.dorm.service.ICollegeService;

import java.util.ArrayList;
import java.util.List;

@Service
public class CollegeServiceImpl implements ICollegeService{

    @Autowired
    private TbCollegeMapper tbCollegeMapper;
    @Autowired
    private ICollegeCacher collegeCacher;

    public boolean addCollege(TbCollege tbCollege) {
        int i = tbCollegeMapper.insertSelective(tbCollege);
        return i>0;
    }

    public TbCollege getCollegeById(Integer id) {
        TbCollege tbCollege = tbCollegeMapper.selectByPrimaryKey(id);
        return tbCollege;
    }

    @Override
    public boolean editCollege(TbCollege tbCollege) {
        int i = tbCollegeMapper.updateByPrimaryKeySelective(tbCollege);
        return i > 0;
    }

    @Override
    public boolean deleteCollege(Integer id) {
        int i = tbCollegeMapper.deleteByPrimaryKey(id);
        return i > 0;
    }

    @Override
    public boolean batchRemoveCollege(List<String> colIds) {
        if (CollectionUtil.isNotEmpty(colIds)) {
            for (String colId : colIds) {
                boolean b = deleteCollege(Integer.valueOf(colId));
                if (!b) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public List<TbCollege> getCollegeByCondition(PageBean pageBean) {
        Page<Object> objects = PageHelper.startPage(pageBean.getPageNum(), pageBean.getLimit());
        List<TbCollege> tbColleges = tbCollegeMapper.getAllCollegeByCondition(pageBean);
        pageBean.setTotal((int) objects.getTotal());
        return tbColleges;
    }

    @Override
    public List<TbCollege> getAllCollege(int all) {
        //TODO 学院管理后台数据访问的话尽量不要缓存,其他管理可以使用缓存
        return tbCollegeMapper.getAllCollege(all);
    }

    //获取所有可用的学院信息--使用缓存
    @Override
    public List<TbCollege> getAllCollege(){
        List<TbCollege> allCollege = collegeCacher.getAllCollege();
        return allCollege;
    }

}
